package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.data.TestData;
import id.net.iconpln.bb.client.model.PageBbListKlarifikasiBean;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.AggregationRenderer;
import com.extjs.gxt.ui.client.widget.grid.AggregationRowConfig;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.grid.SummaryType;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.table.NumberCellRenderer;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;

public class PageTest extends LayoutContainer {
	private ContentPanel cp;
	private EditorGrid laypanel;
	private EditorGrid grid;
	private GroupingStore store;
	
	final NumberFormat currency = NumberFormat.getCurrencyFormat();  
    final NumberFormat number = NumberFormat.getFormat("0.00");  
    final NumberCellRenderer<Grid> numberRenderer = new NumberCellRenderer<Grid>(currency); 
	
	 @Override
	  protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    setLayout(new FitLayout());
	    
	    final ToolBar toolBar = new ToolBar();
		cp = new ContentPanel();

		cp.setLayoutOnChange(true); 
		cp.setCollapsible(false);
		cp.setFrame(false);
		cp.setLayout(new FitLayout());
		cp.setBorders(false);
    	cp.setHeading("Daftar Klarifikasi");
    	toolBar.add(new LabelToolItem("Monitoring Kualitas "));
    	toolBar.add(new SeparatorToolItem());
		cp.setTopComponent(toolBar);
		cp.syncSize();

		laypanel = task();
		cp.add(laypanel);  
		
			
		add(cp, new FitData(5));
	}
		  	
	
	private ContentPanel m(){
		

		return cp;
	}
	private EditorGrid task(){
		   	
			List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
		    
		   	store = new GroupingStore();  
		    getDataGrid();
		   
		    RowNumberer rn = new RowNumberer();  
		    rn.setWidth(30);  
		    configs.add(rn);  
		    
		    ColumnConfig column = new ColumnConfig();  
		    
		    column = new ColumnConfig();  
		    column.setId("nama_surveyor");  
		    column.setHeader("Nama Surveyor");  
		    column.setWidth(300);  
		    configs.add(column);  		  
	
			column = new ColumnConfig();  
		    column.setId("pltu");  
		    column.setHeader("PLTU");  
		    column.setWidth(130);  
		    configs.add(column);  
		    
		    column = new ColumnConfig();  
		    column.setId("alamat");  
		    column.setHeader("Lokasi");    
		    column.setWidth(120);    
		    configs.add(column);
		    
		    column = new ColumnConfig();  
		    column.setId("tglpengambilan");  
		    column.setHeader("Tanggal Pengambilan");    
		    column.setWidth(120);
		    column.setAlignment(HorizontalAlignment.RIGHT);
		    column.setDateTimeFormat(DateTimeFormat.getShortDateTimeFormat());
		    configs.add(column);  
			
	
			column = new ColumnConfig();  
		    column.setId("tglpengujian");  
		    column.setHeader("Tanggal Pengujian");  
		    column.setWidth(120);
		    column.setAlignment(HorizontalAlignment.RIGHT);
		    column.setDateTimeFormat(DateTimeFormat.getShortDateTimeFormat());
		    configs.add(column);   

			column = new ColumnConfig();  
		    column.setId("kualitas_dikontrak");  
		    column.setHeader("Kualitas di Kontrak");
		    TextField<Integer> text = new TextField<Integer>();
		    text.setAllowBlank(false);
		    column.setEditor(new CellEditor(text));
		    column.setWidth(200);  
		    configs.add(column);  

		    column = new ColumnConfig();  
		    column.setId("kualitas_hasiluji");  
		    column.setHeader("Kualitas Hasil Uji");
		    TextField<Integer> text1 = new TextField<Integer>();
		    text1.setAllowBlank(false);
		    column.setEditor(new CellEditor(text1));
		    column.setWidth(200);    
		    configs.add(column);
		    
		    column = new ColumnConfig();  
		    column.setId("kualitas_dipemasok");  
		    column.setHeader("Kualitas di Pemasok");    
		    column.setWidth(200);    
		    configs.add(column);  
			
			column = new ColumnConfig();  
		    column.setId("Klarifikasi");  
		    column.setHeader("Klarifikasi");  
		    column.setWidth(200);  
		    configs.add(column);  
		    
		    ColumnModel cm = new ColumnModel(configs);  
		    
		    // nama_surveyor - colum di mana average di tampilkan
		    AggregationRowConfig arc = new AggregationRowConfig();  
		    arc.setHtml("nama_surveyor", "Average");  
		      
		    // kualitas_dikontrak columm yang mau di average  
		    arc.setSummaryType("kualitas_dikontrak", SummaryType.AVG);  
		    arc.setSummaryFormat("kualitas_dikontrak", NumberFormat.getCurrencyFormat());  
		      
		    // kualitas_hasiluji columm yang mau di average  
		    arc.setSummaryType("kualitas_hasiluji", SummaryType.AVG);  
		    arc.setRenderer("kualitas_hasiluji", new AggregationRenderer() {  
		      public Object render(Number value, int colIndex, Grid grid, ListStore store) {  
		        // you can return html here  
		        return number.format(value.doubleValue());  
		      }  
		    });  
		    cm.addAggregationRow(arc);  
		      
		    arc = new AggregationRowConfig();  
		    arc.setHtml("name", "Maximum");  
		      
		      
		    arc.setSummaryType("kualitas_dikontrak", SummaryType.MAX);  
		    arc.setSummaryFormat("kualitas_dikontrak", NumberFormat.getCurrencyFormat());  
		  
		    arc.setSummaryType("kualitas_dikontrak", SummaryType.MAX);  
		    arc.setRenderer("kualitas_dikontrak", new AggregationRenderer() {  
			public Object render(Number value, int colIndex, Grid grid,
					ListStore store) {
				// TODO Auto-generated method stub
				
				GWT.log("test: " + value.toString(), null);
				
				MessageBox.alert("msg", "test: " + value.toString(), null);
				
				return number.format(value.doubleValue());  
				
			}  
		    });  
		    cm.addAggregationRow(arc); 
		    
		    
		    
		    ListStore<PageBbListKlarifikasiBean> store = new ListStore<PageBbListKlarifikasiBean>();  
		    store.setMonitorChanges(true);  
		    store.add(TestData.getDftKlaf()); 
		    
		  
		    grid = new EditorGrid(store, cm);  
		    grid.setBorders(false);
		    grid.setHideHeaders(false);
		    grid.mask("load Data ....","x-mask-loading");
		    grid.unmask();
		      
		    
		return grid;
	}
	  
	
	private void getDataGrid() {
		
	}
	
	
}